﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using Raven.Client.Document;
using System.Threading;

namespace mrp16062010
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Uretec.AgacCesidi.Duz d = new Uretec.AgacCesidi.Duz();
        }
        PointPairList noktalar = new PointPairList();

        private TreeNode Cizici(Rota.RotaParcasi parca, TreeNode UstNode)
        {
            TreeNode node = new TreeNode(parca.Tanim.ToString() + " " + parca.Adet.ToString());
            if (UstNode!=null)
            {
                UstNode.Nodes.Add(node);
            }
            List<Rota.Operasyon> opList = new List<Rota.Operasyon>();
                opList.AddRange( parca.OperasyonList);
            opList.Reverse();
            foreach (Rota.Operasyon operasyon in opList)
            {
                TreeNode opNode = new TreeNode("Operasyon");
                node.Nodes.Add(opNode);
                foreach (Rota.RotaParcasi altParca in operasyon.AltParca)
                {
                    Cizici(altParca, opNode);
                }
            }
            return node;
        }

        Cizelge c;

        private void button1_Click(object sender, EventArgs e)
        {
            //Veri.UrunTuru ut = new Veri.UrunTuru();
            //List<Veri.UrunTuru> listt= ut.Listele();

            /*
            int[] intInput = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
            string[] stringInput = { "Hello", "World", "Foo" };

            IEnumerable<IEnumerable<int>> liste = PermuteUtils.Permute<int>(intInput, 10);
            
            int s = 0;
            foreach (var item in liste)
            {
                s++;
            }
            */

            OrnekVeri orn = new OrnekVeri();
            
            List<Rota.RotaParcasi> parcaList = new List<Rota.RotaParcasi>();


            Uretec.UrunAgaciTipi urunAgaciTipi = new Uretec.AgacCesidi.Uzun();


            parcaList = new Uretec.UrunAgaciUret().ListeUret(urunAgaciTipi, 10);

            treeView1.Nodes.Add(Cizici(parcaList.First(), null));


            Uretec.SiparisUret siparisUretec = Uretec.SiparisUret.Nesne;

            siparisUretec.GunlukSiparisSayisi = new Uretec.Aralik(2, 5);
            siparisUretec.SiparisTeslimSuresi = new Uretec.Aralik(10, 40);
            siparisUretec.SiparisBuyuklugu = new Uretec.Aralik(10, 50);
            Tarih AnaTarihler = new Tarih(new DateTime(2010,1,1), new DateTime(2010,12,31));
            Uretec.AralikSaat GunlukSaat = new Uretec.AralikSaat(new Uretec.SaatTanim(8,0), new Uretec.SaatTanim(17,30));
            List<Siparis> siparisListe = siparisUretec.Uret(AnaTarihler, GunlukSaat, parcaList);


            List<Uretec.VeriUret> datas = Uretec.VeriUret.Oku();
            Uretec.VeriUret veri = (from x in datas.Skip(0)
                                    select x).First();
            
            /*
            double ortalama = MathNet.Numerics.Statistics.Statistics.Mean(t);

            double stdsapma = MathNet.Numerics.Statistics.Statistics.StandardDeviation(t);
            List<double> zdeg = new List<double>();
            foreach (double item in t)
            {
                double newz = (item - ortalama) / stdsapma;
                zdeg.Add(newz);

            }

            List<double> fzdeg = new List<double>();
            MathNet.Numerics.Distributions.Normal norm = new MathNet.Numerics.Distributions.Normal();
            foreach (double item in zdeg)
            {

                fzdeg.Add(norm.CumulativeDistribution(item));
                noktalar.Add(norm.CumulativeDistribution(item), 20);
            }
            */

            Cizelge ciz = new Cizelge();
            List<IsParcasi> tatilList = new List<IsParcasi>();

            //foreach (IsMerkezi merkez in OrnekVeri.IsMerkeziList)
            //{
            //    IsParcasi tatil = new IsParcasi(new DateTime(2010, 08, 18, 10, 0, 0), new DateTime(2010, 08, 18, 10, 30, 0));
            //    tatil.TatilMi = true;
            //    tatil.TatilIsMerkezi = merkez;
            //    tatilList.Add(tatil);
            //}
            //ciz.TatilEkle(tatilList);

            ciz.Id = "Cizelge/1";
            int iii = 0;
            /*
            foreach (Rota.RotaParcasi item in parcaList)
            {
                iii++;
                Tarih tar = new Tarih(new DateTime(2010, 08, 18, 8, 0, 0), new DateTime(2010, 08, 18, 8, 0, 0));
                Siparis sip = new Siparis(iii, item, 1, tar);
                Yukle yuk = new Yukle(ciz, sip);
            }*/
            Tarih tar2 = new Tarih(new DateTime(2010, 08, 18, 8, 0, 0), new DateTime(2010, 08, 18, 8, 0, 0));

            Tarih tatilTarihleri = new Tarih();
            tatilTarihleri.BaslangicTarihi = tar2.BaslangicTarihi;
            tatilTarihleri.BitisTarihi = new DateTime(2010, 12, 30);
            List<Uretec.AralikSaat> TatilAraliklari = new List<Uretec.AralikSaat>();
            Uretec.TatilUret tatilUret = new Uretec.TatilUret(TatilAraliklari, Ayarlar.Nesne.CalismaSaatleri, tatilTarihleri);
            List<IsParcasi> TatilList = tatilUret.Olustur();
            ciz.TatilEkle(TatilList);


            Siparis sip2 = new Siparis(10, veri.parcaListKarisim.First(), 10, tar2);
            Yukle yuk2 = new Yukle(ciz, sip2);

            

            //Tarih tar3 = new Tarih(new DateTime(2010, 08, 18, 8, 0, 0), new DateTime(2010, 08, 18, 8, 0, 0));
            //Siparis sip3 = new Siparis(100, OrnekVeri.OrnekRota, 20, tar3);
            //Yukle yuk3 = new Yukle(ciz, sip3);

            Data.simulasyonEntities db = new Data.simulasyonEntities();
            foreach (IsParcasi item in yuk2.SiparisBilgi.IsParcalari)
            {
                Data.IsParcasi parca = new Data.IsParcasi();
                parca.CalismaId = 9998;
                parca.CizelgeBaslangic = item.BasTarihi;
                parca.CizelgeBitis = item.BitTarihi;
                parca.MakineNo = item.IM.Kod;
                parca.Uzunluk = Convert.ToInt32(item.Uzunluk.TotalMinutes);
                db.AddToIsParcasi(parca);
            }
            db.SaveChanges();

            //Similasyon
            Simulation.Ortam ortam = new Simulation.Ortam();
            ortam.KuyrukSiralamaTipi = Simulation.Kuyruk.IslemTipi.CriticalRatio;
            ortam.BaslangicTarihi = new DateTime(2010, 01, 01, 8, 0, 0);
            foreach (IsMerkezi merkez in ciz.IsMerkezleri)
            {
                Simulation.Makine yeniMakine = new Simulation.Makine();
                yeniMakine.Merkez = merkez;
                yeniMakine.BulunduguOrtam = ortam;
                yeniMakine.Kod = merkez.Kod;
                ortam.MakineEkle(yeniMakine);
            }
            //Ortama tatilleri ata
            ortam.TatilAta(tatilList);

            List<IsParcasi> liste = new List<IsParcasi>();
            foreach (CizelgeIsMerkezi cim in ciz.Merkezler)
            {
                List<IsParcasi> merkezParcalar = (from x in cim.IsListe
                                                  where x.ArkasindakiIsParcalari.Count == 0 && x.TatilMi == false
                                                  orderby x.BasTarihi ascending
                                                  select x).ToList();

                liste.AddRange(merkezParcalar);
            }
            liste = liste.OrderBy(p => p.BasTarihi).ToList();
            ortam.ParcaAta(liste);


            //Similasyon bitiş

            /*RavenDB*/
            /*
            var store = new DocumentStore { Url = "http://localhost:8080" };
            
            store.Initialize();
            var session = store.OpenSession();
            session.Store(ciz);
            session.SaveChanges();
            */
            //Thread.Sleep(30000);
            c = ciz;
            grafik();
           
        }

        private void grafik()
        {


            // get a reference to the GraphPane

            GraphPane myPane = zgc.GraphPane;

            // Set the Titles

            myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)";
            myPane.XAxis.Title.Text = "My X Axis";
            myPane.YAxis.Title.Text = "My Y Axis";

            // Make up some data arrays based on the Sine function

            double x, y1, y2;
            PointPairList list1 = new PointPairList();
            PointPairList list2 = new PointPairList();
            for (int i = 0; i < 36; i++)
            {
                x = (double)i + 5;
                y1 = 1.5 + Math.Sin((double)i * 0.2);
                y2 = 3.0 * (1.5 + Math.Sin((double)i * 0.2));
                list1.Add(x, y1);
                list2.Add(x, y2);
            }

            // Generate a red curve with diamond

            // symbols, and "Porsche" in the legend

            LineItem myCurve = myPane.AddCurve("Porsche",
                  noktalar, Color.Red, SymbolType.Diamond);

            // Generate a blue curve with circle

            // symbols, and "Piper" in the legend
            /*
            LineItem myCurve2 = myPane.AddCurve("Piper",
                  list2, Color.Blue, SymbolType.Circle);
            */
            // Tell ZedGraph to refigure the

            // axes since the data have changed

            zgc.AxisChange();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CizelgeCizim.cizelgecizim cizim = new CizelgeCizim.cizelgecizim(c, true);
            cizim.Show();
            CizelgeCizim.cizelgecizim cizim2 = new CizelgeCizim.cizelgecizim(c, false);
            cizim2.Show();
            LogForm log = new LogForm();
            log.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<Uretec.VeriUret> datas = Uretec.VeriUret.Oku();
            Uretec.VeriUret veri = (from x in datas.Skip(0)
                                    select x).First();
            treeView1.Nodes.Add(Cizici(veri.parcaListKarisim.First(), null));
            
        }
    }
}
